
define([
        "jquery",
        "IO",
        "UPLOADUI",
        "UPLOAD",
        "jqueryUI",
        "bootbox"
    ], function ($, IO, upui, up, jui, bootbox, definebt) {
    var  paper_id;
        $(function () {

            gettable();
            function gettable() {
                var grid;
                //每列数据列性
                var columns = [
                    {
                        title: '编号',
                        dataIndex: 'paper_id',
                        dataSource: function (data, datarow, gridobj, current_column) {
                            return "<span>" + data[datarow].paper_id + "</span>";
                        }
                    },
                    {
                        title: '名称',
                        dataIndex: 'paper_name',
                        dataSource: function (data, datarow, gridobj, current_column) {
                            return "<span>" + data[datarow].paper_name + "</span>";
                        }
                    },
                    {
                        title: '发布状态',
                        dataIndex: 'paper_status',
                        dataSource: function (data, datarow, gridobj, current_column) {
                            if(data[datarow].paper_status==0){
                                return "<span style='color: red;'>未发布</span>";
                            }else if(data[datarow].paper_status==1){
                                return "<span style='color: green;'>已发布</span>";
                            }

                        }
                    },
                    {
                        title: '试卷题目总数',
                        dataIndex: 'question_count',
                        dataSource: function (data, datarow, gridobj, current_column) {

                            return "<span>" + data[datarow].question_count + "</span>";
                        }
                    },
                    {
                        title: '未提交人数',
                        dataIndex: 'no_answer_people_count',
                        dataSource: function (data, datarow, gridobj, current_column) {

                            return "<span>" + data[datarow].no_answer_people_count + "</span>";
                        }
                    },
                    {
                        title: '创建时间',
                        dataIndex: 'no_answer_people_count',
                        dataSource: function (data, datarow, gridobj, current_column) {

                            return "<span>" +new Date(parseInt(data[datarow].timestamp)).toLocaleString().replace(/:\d{1,2}$/,' ') + "</span>";
                        }
                    },
                    {
                        title: '操作',
                        dataIndex: 'action',
                        dataSource: function (data, datarow, gridobj, current_column) {
                            return "<span><a href='#' class='editor' style='color: green;' paper_id='" + data[datarow].paper_id + "' paper_name='" + data[datarow].paper_name + "'>编辑</a>&nbsp;&nbsp;&nbsp;&nbsp;<a href='#' class='delete' style='color: red;' paper_id='" + data[datarow].paper_id + "'>删除</a>&nbsp;&nbsp;&nbsp;&nbsp;<a download href='http://zjprod.snsunion.cn:8081/CorporateTraining/api/v1/papers/export?paper_id="+data[datarow].paper_id+"' style='color: green;' class='export'>导出答案</a>&nbsp;&nbsp;&nbsp;&nbsp;<a href='#' class='into' style='color: green;' paper_id='" + data[datarow].paper_id + "'>试卷管理</a></span>";
                        }
                    },

                ];
                //数组初始化
                function dataInit() {
                    grid = new Grid("/papersList", $("#table_my_toast"), $("#paging_my_toast"), 1, 10, columns, get_input_param(), 10);
                    //初始化
                    grid.Init();
                }

                //获取输入框内容
                function get_input_param() {

                    return "";
                }

                dataInit();
            }
            //点击新增咨询按钮
            $('#btnAddMaths').on('click',function () {
                $('#myModal1').modal("show");
            })
            //点击新增咨询确定按钮
             $('#btnAddMathsSure').on('click',function () {
                var  paper_name=$('#MathsName').val();
                if(paper_name===""){
                    bootbox.alert("请输入课程名称!");
                    return;
                }
                 var param={};
                 param.paper_name=paper_name;
                 IO.get("/papersAdd", param, function (d) {
                  if(d.code==0){
                      $('#myModal1').modal('hide');
                      bootbox.alert('课程新增成功',function () {
                          gettable();
                      })
                      
                  }else{
                      bootbox.alert('课程新增失败',function () {
                          $('#myModal1').modal('hide');
                      });
                  }
                 });
                
            })

            //删除课程
            $(document).on('click','.delete',function () {
                paper_id= $(this).attr('paper_id');//课程id
                var param={};
                param.paper_id=paper_id;
                bootbox.confirm("确定要删除该课程?",function(result){
                    if(result) {
                        IO.get('/papersDelete',param,function (d) {
                            if (d.code==0){
                                bootbox.alert('课程删除成功',function () {
                                    gettable();
                                })
                            }else{
                                bootbox.alert('课程删除失败')
                            }
                        })
                    }})

            })


            //点击课程编辑
            $(document).on('click','.editor',function () {
                paper_id=$(this).attr('paper_id');//课程id
                var paper_name=$(this).attr('paper_name');//课程名
                $('#MathsNameUpdate').val(paper_name);
                $('#myModal2').modal("show");

            })

            // 修改课程
            $('#btnUpdateMathsSure').on('click',function () {
                var paper_name=$('#MathsNameUpdate').val();
                if(paper_name==''){
                    bootbox.alert('课程名称不能为空')
                    return;
                }
               var param={};
                param.paper_id=paper_id;
                param.paper_name=paper_name;
                IO.get('/papersUpdate',param,function (d) {
                    if(d.code==0){
                        $('#myModal2').modal("hide");
                        bootbox.alert('修改成功',function () {
                            gettable();
                        });

                    }else{
                        bootbox.alert('修改失败');
                    }
                })



            })
            
            //课程打分
            $(document).on('click','.marks',function () {
                paper_id=$(this).attr('paper_id');
                location.href='/checkMark?paper_id='+paper_id;
            })

            //管理试卷
            $(document).on('click','.into',function () {
                var paper_id= $(this).attr('paper_id');//课程id
                location.href='/checkMaths?paper_id='+paper_id;
            })

            //模态框隐藏1
            $('#myModal1').on('hide.bs.modal', function () {
                $('#MathsName').val('');
            })
            //模态框隐藏2
            $('#myModal2').on('hide.bs.modal', function () {
                $('#MathsNameUpdate').val('');
            })

        })
    }

)
